package com.th.supcom.pds.drugreturn.server;

import java.util.ArrayList;
import java.util.List;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import com.th.supcom.pds.common.enums.BatchNumberManagerFlag;
import com.th.supcom.pds.domain.pojo.PdsPrescDetailCombine;
import com.th.supcom.pds.domain.vo.PatientInfo;
import com.th.supcom.pds.domain.vo.PdsPrescDetailAndPrescVo;
import com.th.supcom.pds.domain.vo.PrescAndPatiResult;
import com.th.supcom.pds.drugreturn.client.service.DrugReturnRpc;
import com.th.supcom.pds.internal.api.DrugReturnService;
import com.th.supcom.pds.internal.api.PdsRecipefeeService;
import com.th.supcom.pds.internal.client.model.MedicareCardAllInfoModel;
import com.th.supcom.pds.internal.client.model.MedicareCardPatientInfoModel;
import com.th.supcom.pds.internal.client.model.PrescAndPatResultModel;
import com.th.supcom.pds.sortingdrug.client.model.PdsPrescDetailInfoModel;

/**
 * 门诊退药控制层.
 * 
 * @author yzhang
 */
public class DrugReturnServiceController extends GwtController implements DrugReturnRpc
{
    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private static final long serialVersionUID = -8951396589889576869L;

    private DrugReturnService m_drugReturnService;

    private PdsRecipefeeService m_pdsRecipefeeService;

    /**
     * 默认构造函数.
     */
    public DrugReturnServiceController ()
    {

    }

    /**
     * 门诊退药通过
     */
    @Override
    public void drugReturnPass (final List <PdsPrescDetailInfoModel> prescModels,
                                final List <PdsPrescDetailInfoModel> prescDetailModels, final String operator)
                                                                                                              throws UiException
    {
        final List <PdsPrescDetailAndPrescVo> prescLists = ConvertUtils.convert (PdsPrescDetailAndPrescVo.class,
                                                                                 prescModels);
        final List <PdsPrescDetailAndPrescVo> prescDetailLists = ConvertUtils.convert (PdsPrescDetailAndPrescVo.class,
                                                                                       prescDetailModels);
        try
        {
            m_drugReturnService.drugReturnPass (prescLists, prescDetailLists, operator);
        }
        catch (final Exception e)
        {
            throw new UiException (e.getMessage (), e);
        }
    }

    /**
     * 门诊退药被拒绝
     */
    @Override
    public void drugReturnRefuse (final List <PdsPrescDetailInfoModel> prescModels,
                                  final List <PdsPrescDetailInfoModel> prescDetailModels, final String operator)
                                                                                                                throws UiException
    {
        final List <PdsPrescDetailAndPrescVo> prescLists = ConvertUtils.convert (PdsPrescDetailAndPrescVo.class,
                                                                                 prescModels);
        final List <PdsPrescDetailAndPrescVo> prescDetailLists = ConvertUtils.convert (PdsPrescDetailAndPrescVo.class,
                                                                                       prescDetailModels);
        try
        {
            m_drugReturnService.drugReturnRefuse (prescLists, prescDetailLists, operator);
        }
        catch (final Exception e)
        {
            throw new UiException (e.getMessage (), e);
        }
    }

    /**
     * 通过处方号查找处方明细以及处方记录 和患者记录
     */
    @Override
    public PrescAndPatResultModel findPrescDetailByPrescList (List <String> prescNoList, String performedBy)
                                                                                                            throws UiException
    {
        List <List <PdsPrescDetailAndPrescVo>> lists;
        PrescAndPatResultModel prescAndPatResultModel = new PrescAndPatResultModel ();
        List <List <PdsPrescDetailInfoModel>> modelList = new ArrayList <List <PdsPrescDetailInfoModel>> ();
        // 存放主处方数据
        final List <PdsPrescDetailInfoModel> prescs = new ArrayList <PdsPrescDetailInfoModel> ();
        // 处方处方明细数据
        final List <PdsPrescDetailInfoModel> prescDetails = new ArrayList <PdsPrescDetailInfoModel> ();
        PrescAndPatiResult prescAndPatiResult = new PrescAndPatiResult ();
        try
        {
            prescAndPatiResult = m_drugReturnService.getPrescDetailForReturn (prescNoList, performedBy);
            lists = prescAndPatiResult.getList ();
        }
        catch (final Exception e)
        {
            throw new UiException (e.getMessage (), e);
        }
        if (lists == null || lists.isEmpty ())
        {
            return prescAndPatResultModel;
        }

        for (List <PdsPrescDetailAndPrescVo> prescVoList : lists)
        {
            List <PdsPrescDetailInfoModel> prescModel = ConvertUtils.convert (PdsPrescDetailInfoModel.class,
                                                                              prescVoList);

            // 设置每条明细的金额
            for (final PdsPrescDetailInfoModel model1 : prescModel)
            {
                final PdsPrescDetailCombine pdsprescDetailCombine = new PdsPrescDetailCombine ();
                pdsprescDetailCombine.setPhamCode (model1.getPhamStdCode ());
                pdsprescDetailCombine.setPerformedBy (performedBy);
                String phamBatchNumberBatchFalg = "0";
                try
                {
                    // phamBatchNumberBatchFalg =
                    // m_pdsRecipefeeService.findDrugBatchFlagByCode
                    // (pdsprescDetailCombine);
                    // 批次检查
                    if (BatchNumberManagerFlag.OnBatchManager.getEnumCode ().equals (phamBatchNumberBatchFalg))
                    {
                        model1.setBatchNumberManagerFlag (phamBatchNumberBatchFalg);
                    }
                    else
                    {
                        model1.setBatchNumberManagerFlag (BatchNumberManagerFlag.UnBatchManager.getEnumCode ());
                    }
                }
                catch (final Exception e)
                {
                    throw new UiException (e.getMessage (), e);
                }
            }
            Double sum = 0.0;
            // 设置处方的总金额
            for (final PdsPrescDetailInfoModel model2 : prescModel)
            {
                sum += model2.getCosts ();
            }
            if (!prescModel.isEmpty ())
            {
                prescModel.get (0).setTotalPrice (sum);
            }
            prescs.add (prescModel.get (0));
            prescDetails.addAll (prescModel);
        }
        modelList.add (prescs);
        modelList.add (prescDetails);
        prescAndPatResultModel.setPdsPrescDetailInfoModellist (modelList);

        // 患者信息转换
        /*
         * if (null != prescAndPatiResult.getPatientInfo ()) { PatientInfoModel
         * medicareCardAllInfoModel = convertmedicareCard
         * (prescAndPatiResult.getPatientInfo ());
         * prescAndPatResultModel.setMedicareCardAllInfoModel
         * (medicareCardAllInfoModel);
         * 
         * }
         */
        return prescAndPatResultModel;
    }

    private MedicareCardAllInfoModel convertmedicareCard (PatientInfo patientIno)
    {

        MedicareCardAllInfoModel medicareCardAllInfoModel = new MedicareCardAllInfoModel ();
        MedicareCardPatientInfoModel medicareCardPatientInfoModel = new MedicareCardPatientInfoModel ();
        medicareCardPatientInfoModel.setName (patientIno.getName ());
        medicareCardPatientInfoModel.setSex (patientIno.getSex ());
        medicareCardPatientInfoModel.setDateOfBirth (patientIno.getBirthDate ());
        medicareCardPatientInfoModel.setPatientId (patientIno.getPatiId ());
        medicareCardAllInfoModel.setCardBalance (patientIno.getBalance ());
        medicareCardPatientInfoModel.setUpdateTime (patientIno.getVisitBeginDate ());
        medicareCardPatientInfoModel.setChargeType (patientIno.getChargeType ());
        medicareCardPatientInfoModel.setVisitId (patientIno.getVisitId ());
        medicareCardPatientInfoModel.setDeptName (patientIno.getDeptName ());
        medicareCardPatientInfoModel.setVisitDay (patientIno.getVisitDay ());
        medicareCardPatientInfoModel.setBedCode (patientIno.getBedCode ());
        medicareCardAllInfoModel.setMedicareCardPatientInfo (medicareCardPatientInfoModel);
        return medicareCardAllInfoModel;
    }

    public void setDrugReturnService (final DrugReturnService drugReturnService)
    {
        m_drugReturnService = drugReturnService;
    }

    public void setPdsRecipefeeService (final PdsRecipefeeService pdsRecipefeeService)
    {
        m_pdsRecipefeeService = pdsRecipefeeService;
    }

}